/*
 * Copyright 2019-2025 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * https://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.iiifi.kite.core.util;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import lombok.experimental.UtilityClass;

/**
 * List工具类
 *
 * @author kite@iiifi.com 花朝
 */
@UtilityClass
public class ListUtils extends org.apache.commons.collections4.ListUtils {

    /**
     * 创建一个空ArrayList
     *
     * @param <T> 元素类型
     * @return 空的List
     */
    public static <T> List<T> newArrayList() {
        return new ArrayList<>();
    }

    /**
     * 创建一个ArrayList
     *
     * @param elements 数组
     * @param <T> 集合元素类型
     * @return List<T>
     */
    @SafeVarargs
    public static <T> List<T> newArrayList(T... elements) {
        List<T> arrayList = new ArrayList<>(elements.length);
        Collections.addAll(arrayList, elements);
        return arrayList;
    }

    /**
     * 创建一个空LinkedList
     *
     * @param <T> 元素类型
     * @return 空的List
     */
    public static <T> List<T> newLinkedList() {
        return new LinkedList<>();
    }

    /**
     * 创建一个LinkedList
     *
     * @param elements 数组
     * @param <T> 集合元素类型
     * @return List<T>
     */
    @SafeVarargs
    public static <T> List<T> newLinkedList(T... elements) {
        List<T> linkedList = new LinkedList<>();
        Collections.addAll(linkedList, elements);
        return linkedList;
    }

    /**
     * 判断list是否为空如果为null,new一个新的ArrayList
     *
     * @param list list
     * @return 非null的List
     */
    public static <T> List<T> defaultList(List<T> list) {
        return (CollectionUtils.isEmpty(list) ? new ArrayList<>() : list);
    }

    /**
     * 安全的截取list
     *
     * @param list 原 list
     * @param start 开始位置
     * @param end 结束位置
     * @param <T> T
     * @return 新的list
     */
    public static <T> List<T> safeSubList(List<T> list, int start, int end) {
        if (start >= list.size()) {
            return new ArrayList<>();
        }
        return list.subList(Math.max(start, 0), Math.min(end, list.size()));
    }

    /**
     * 获取第一个元素,空集合返回null
     *
     * @param list list
     * @param <T> T
     * @return T
     */
    public static <T> T getFirst(List<T> list) {
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    /**
     * 获取最后一个元素,空集合返回null
     *
     * @param list list
     * @param <T> T
     * @return T
     */
    public static <T> T getLast(List<T> list) {
        return CollectionUtils.isEmpty(list) ? null : list.get(list.size() - 1);
    }

    /**
     * 去重
     *
     * @param list list
     * @param <T> T
     * @return List<T>
     */
    public static <T> List<T> unique(List<T> list) {
        return CollectionUtils.isEmpty(list) ? new ArrayList<>() : new ArrayList<>(new LinkedHashSet<>(list));
    }

    /**
     * 将list 转为 map
     *
     * @param list list
     * @param function function
     * @param <K> key
     * @param <V> value
     * @return map
     */
    public static <K, V> Map<K, V> toMap(List<V> list, Function<V, K> function) {
        if (CollectionUtils.isEmpty(list) || function == null) {
            return new HashMap<>();
        }
        return list.stream().collect(Collectors.toMap(function, Function.identity(), (v1, v2) -> v1));
    }
}
